package com.example.springbootexamples;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchDemo {

    private Logger logger= LoggerFactory.getLogger(this.getClass());


    class R implements Runnable{

        private CountDownLatch countDownLatch;

        public R(CountDownLatch countDownLatch) {
            this.countDownLatch = countDownLatch;
        }

        @Override
        public void run() {
            logger.info("---------------------");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            logger.info("-=============");

            countDownLatch.countDown();

        }
    }

    @Test
    public void t(){
        int n=3;
        CountDownLatch countDownLatch =new CountDownLatch(n);

        for(int i=0;i<n;i++){
            new Thread(new R(countDownLatch)).start();
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        logger.info("tttttttttttttttttttttttttttttt");


    }
}
